﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using NCT.Framework.Database;
using NCT.Framework;
using System.Data;

namespace VISS.API.Users {
    class UserFollowerBC : IUserFollower {

        #region IUserFollower Members

        public bool Insert(UserFollowerEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                item.DateCreated = DateTime.Now;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Insert to DB
                result = UserFollowerDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Insert into Cache
                if (ConfigInfo.UseCache && result) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFollowerBC.Insert({0}): \n" + ex.ToString(), SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public bool Delete(string followerUserKey, string followedUserKey) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Delete from DB
                result = UserFollowerDA.Delete(followerUserKey, followedUserKey, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Delete from Cache
                if (ConfigInfo.UseCache && result) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFollowerBC.Delete({0}, {1}): \n" + ex.ToString(), 
                    followerUserKey,
                    followerUserKey));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public List<UserFollowerEnt> GetByFollowerUserKey(string followerUserKey) {
            List<UserFollowerEnt> result = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                if (ConfigInfo.UseCache) {
                }
                else {
                    result = UserFollowerDA.GetByFollowerUserKey(followerUserKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFollowerBC.GetByFollowerUserKey({0}): \n" + ex.ToString(), followerUserKey));
            }
            return result;
        }

        public List<UserFollowerEnt> GetByFollowedUserKeyWithPaging(string followedUserKey, int page, int recordPerPage, out int totalRecord) {
            List<UserFollowerEnt> result = new List<UserFollowerEnt>();
            totalRecord = 0;
            try {
                if (ConfigInfo.UseCache) {

                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    totalRecord = UserFollowerDA.CountByFollowedUserKey(followedUserKey, connectionName, null, null);
                    if (totalRecord == 0) {
                        return result;
                    }

                    int totalPage = (int)(totalRecord / recordPerPage);
                    if (totalRecord % recordPerPage != 0) {
                        totalPage++;
                    }
                    if (page > totalPage) {
                        return result;
                    }

                    long startRow = (page - 1) * recordPerPage + 1;
                    long endRow = startRow + recordPerPage - 1;

                    if (endRow > totalRecord) {
                        endRow = totalRecord;
                    }

                    result = UserFollowerDA.GetByFollowedUserKeyWithPaging(followedUserKey, startRow, endRow, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFollowerBC.GetByFollowedUserKeyWithPaging({0}, {1}, {2}): \n" + ex.ToString(),
                                                followedUserKey,                            
                                                page,
                                                recordPerPage));
                totalRecord = 0;
            }
            return result;
        }

        public int CountByFollowedUserKey(string followedUserKey) {
            int result = 0;
            try {
                if (ConfigInfo.UseCache) {

                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    result = UserFollowerDA.CountByFollowedUserKey(followedUserKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFollowerBC.CountByFollowedUserKey({0}): \n" + ex.ToString(), followedUserKey));
                result = 0;
            }

            return result;
        }

        public int CountByFollowerUserKey(string followerUserKey){
            int result = 0;
            try
            {
                if (ConfigInfo.UseCache) {

                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    result = UserFollowerDA.CountByFollowerUserKey(followerUserKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex)
            {
                LogUtils.Error(string.Format("VISS.Users.UserFollowerBC.CountFollowerUserKey({0}): \n" + ex.ToString(), followerUserKey));
                result = 0;
            }

            return result;
        }

        public bool CheckExist(string followerUserKey, string followedUserKey) {
            bool result = false;
            try {
                if (ConfigInfo.UseCache) {

                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    result = UserFollowerDA.CheckExist(followerUserKey, followedUserKey, connectionName, null, null) > 0;
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserFollowerBC.CheckIfUser1FollowedUser2({0}, {1}): \n" + ex.ToString(), 
                    followerUserKey,
                    followedUserKey));
                result = false;
            }

            return result;
        }

        #endregion
    }
}
